package com.walnut.servlet;


import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.walnut.dao.FoodDao;
import com.walnut.dao.impl.FoodDaoImpl;
import com.walnut.pojo.Food;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/selectFood")
public class SelectFoodServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = request.getServletContext();
        FoodDao foodDao = new FoodDaoImpl();

        // 获取查询参数
        String foodName = request.getParameter("foodName");
        String priceStr = request.getParameter("price");
        String foodStatus = request.getParameter("foodStatus");

        // 按照给定的条件查询食品
        List<Food> foodList = new ArrayList<>();
        try {
            foodList = foodDao.selectByConditions(foodName, priceStr, foodStatus);

            // 创建 HTML 表格内容
            StringBuilder tableContent = new StringBuilder();

            if (foodList != null && !foodList.isEmpty()) {
                if (context.getAttribute("isAdmin").equals("1")){
                    int index = 1;
                    for (Food food : foodList) {
                        tableContent.append("<tr>")
                                .append("<td>").append(index).append("</td>")
                                .append("<td><img src='").append(food.getFoodImg()).append("' alt='").append(food.getFoodName()).append("'></td>")
                                .append("<td>").append(food.getFoodName()).append("</td>")
                                .append("<td>").append(food.getPrice()).append("</td>")
                                .append("<td>").append(food.getFoodStatus()).append("</td>")
                                .append("<td class='actions'>")
                                .append("<a href='/selectFood?foodId=").append(food.getFoodId()).append("&_t=").append(System.currentTimeMillis()).append("'>修改</a>")
                                .append("<a href='javascript:void(0);' class='delete' onclick='confirmDelete(").append(food.getFoodId()).append(")'>删除</a>")
                                .append("</td>")
                                .append("</tr>");
                        index++;
                    }
                }else{
                    int index = 1;
                    for (Food food : foodList) {
                        tableContent.append("<tr>")
                                .append("<td>").append(index).append("</td>")
                                .append("<td><img src='").append(food.getFoodImg()).append("' alt='").append(food.getFoodName()).append("'></td>")
                                .append("<td>").append(food.getFoodName()).append("</td>")
                                .append("<td>").append(food.getPrice()).append("</td>")
                                .append("<td>").append(food.getFoodStatus()).append("</td>")
                                .append("<td class='actions'>")
                                .append("<a href='javascript:void(0);' class='purchase' onclick='confirmPurchase(").append(food.getFoodId()).append(")'>购买</a>")
                                .append("</td>")
                                .append("</tr>");
                        index++;
                    }
                }

            } else {
                tableContent.append("<tr><td colspan='6'>没有找到相关食品。</td></tr>");
            }
            // 输出 HTML 片段
            response.getWriter().write(tableContent.toString());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 暂时不需要处理 POST 请求
    }
}
